<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function fertilization_list_page()
{
    @$output = drupal_render(drupal_get_form('fertilization_list_form'));
    return $output;
}

function fertilization_list_form($form, &$form_state)
{
    @$plant_info_id = $_GET['plant_info_id'];
    @$soil_info_id = $_GET['soil_info_id'];
    if (!empty($plant_info_id)) {
        setcookie("fertilization_table_name", 'fertilization_info', time() + 2 * 3600);
        $form_state['table_name'] = 'fertilization_info';
        $form_state['pid_name'] = 'plant_info_id';
        $form_state['pid_value'] = $plant_info_id;
    } elseif (!empty($soil_info_id)) {
        setcookie("fertilization_table_name", 'fertilizer_recommendation', time() + 2 * 3600);
        $form_state['table_name'] = 'fertilizer_recommendation';
        $form_state['pid_name'] = 'soil_info_id';
        $form_state['pid_value'] = $soil_info_id;
    } else {
        drupal_set_message(t('错误的参数！'), 'status', FALSE);
        drupal_goto('farmer/plant_info/list');
    }
    if (!empty($plant_info_id)) {
        $query = db_select( 'plant_info', 'T');
        $query->join('plant', 'P', 'P.plant_id=T.plant_id');
        $query->addField('P', 'plant_name');
    } else{
        $query = db_select( 'soil_info', 'T');
    }
    $query->join('farmer', 'F', 'F.farmer_id=T.farmer_id');
    $query->join('addr', 'A', 'F.addr_id=A.addr_id');
    $query->condition('T.'. $form_state['pid_name'], $form_state['pid_value'], '=');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('F', 'farmer_name');
    $result = $query->execute()->fetchObject();
    $form['province'] = array(
      '#type' => 'textfield',
      '#default_value' => $result->province,
      '#size' => 32,
      '#maxlength' => 32,
      '#disabled' => TRUE,
    );
    $form['city'] = array(
      '#type' => 'textfield',
      '#default_value' => $result->city,
      '#size' => 32,
      '#maxlength' => 32,
      '#disabled' => TRUE,
    );
    $form['county'] = array(
      '#type' => 'textfield',
      '#default_value' => $result->county,
      '#size' => 32,
      '#maxlength' => 32,
      '#disabled' => TRUE,
    );
    $form['farmer'] = array(
      '#type' => 'textfield',
      '#default_value' => $result->farmer_name,
      '#size' => 32,
      '#maxlength' => 32,
      '#disabled' => TRUE,
    );
    if (!empty($plant_info_id)) {
        $form['plant'] = array(
          '#type' => 'textfield',
          '#default_value' => $result->plant_name,
          '#size' => 32,
          '#maxlength' => 32,
          '#disabled' => TRUE,
        );
    }
    $form['table']['#markup'] =data_list_view_page($form, $form_state);
    return $form;
}

function fertilization_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '施肥种类', 'class' => ['essential type']),
        array('data' => '肥料名称', 'class' => ['essential fertilizer_name']),
        array('data' => '用量', 'class' => ['essential fertilizer_quantity']),
        array('data' => '施肥日期', 'class' => ['essential fertilization_date']),
        array('data' => '施肥方法', 'class' => ['essential fertilization_method']),
        array('data' => '备注', 'class' => ['essential comments']),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $query = db_select($form_state['table_name'], 'F')->extend('PagerDefault')->extend('TableSort');
    $query->condition('F.' . $form_state['pid_name'], $form_state['pid_value']);
    $query->fields('F');
    $query->limit(variable_get("list_page_item_number", 5));
    $fertilizationInfo = $query->orderByHeader($header)->execute();

    $rows = array();
    //表内容
    $n = 1;
    foreach ($fertilizationInfo as $row) {
        $rows[] = array(
            array('data' => "<input type='checkbox' class='listViewTableCheck' value ='" . $row->id . "' name='" . $row->id . "'/>" . $n++, 'class' => 'essential no'),
            array('data' => $row->type, 'class' => ['essential type']),
            array('data' => $row->fertilizer_name, 'class' => ['essential fertilizer_name']),
            array('data' => $row->fertilizer_quantity, 'class' => ['essential fertilizer_quantity']),
            array('data' => $row->fertilization_date, 'class' => ['essential fertilization_date']),
            array('data' => $row->fertilization_method, 'class' => ['essential fertilization_method']),
            array('data' => $row->comments, 'class' => ['essential comments']),
            array('data' => '<a href="'.base_path().'farmer/fertilization/edit?' . $form_state['pid_name'] . '=' . $form_state['pid_value'] . "&id=" . $row->id . '" data-toggle="modal" target="_blank"><img width="20px" height="20px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
        );
    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    /*	 * *按钮** */
    if(sizeof($rows) > 0) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;

}

function fertilization_delete()
{
    if (!empty($_COOKIE['fertilization_table_name'])) {
        $table_name = $_COOKIE['fertilization_table_name'];
        ajax_table_delete($table_name, 'id');
    }
}